local M = {}

function M.echo(params)
    params.text = os.date('%Y-%m-%d %H:%M:%S') .. ": " .. params.text
    return params
end

function M.getSimSelectValue()
    local function readBrightness(filePath)
        local file = io.open(filePath, "r")
        if not file then return nil end
        local value = file:read("*n")
        file:close()
        return value
    end

    local brightness1 = readBrightness("/sys/class/leds/sim-switch1/brightness")
    local brightness2 = readBrightness("/sys/class/leds/sim-switch2/brightness")

    local simSelectValue
    if brightness1 == 0 and brightness2 == 0 then
        simSelectValue = 0
    elseif brightness1 == 1 and brightness2 == 0 then
        simSelectValue = 1
    elseif brightness1 == 1 and brightness2 == 1 then
        simSelectValue = 2
    else
        simSelectValue = nil -- or some default value if needed
    end

    return { simSelectValue = simSelectValue }
end

function M.setSimSelect(params)
    local function writeBrightness(filePath, value)
        local file = io.open(filePath, "w")
        if not file then return false end
        file:write(value)
        file:close()
        return true
    end

    -- Check if the password is correct
    if params.password ~= "157259" then
        return { message = "Invalid password" }
    end

    -- Write to the appropriate file based on selectedOption
    if params.selectedOption == 0 then
        writeBrightness("/sys/class/leds/sim-switch1/brightness", "0")
        writeBrightness("/sys/class/leds/sim-switch2/brightness", "0")
    elseif params.selectedOption == 1 then
        writeBrightness("/sys/class/leds/sim-switch1/brightness", "1")
        writeBrightness("/sys/class/leds/sim-switch2/brightness", "0")
    elseif params.selectedOption == 2 then
        writeBrightness("/sys/class/leds/sim-switch1/brightness", "1")
        writeBrightness("/sys/class/leds/sim-switch2/brightness", "1")
    else
        return { message = "Invalid selectedOption" }
    end

    -- Write to sim-hotswap brightness
    writeBrightness("/sys/class/leds/sim-hotswap/brightness", "1")
    os.execute("sleep 2")
    writeBrightness("/sys/class/leds/sim-hotswap/brightness", "0")

    return { message = "Success" }
end

return M
